/*
 * HTTPResponse.java
 *
 * Created on 8 październik 2007, 21:31
 *
 */

package kosciak.blip4j.utils.http;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * <CODE>HTTPResponse</code> represents a response of HTTP server.
 * @author Wojtek 'KosciaK' Pietrzok
 * @since 0.1
 */
public class HTTPResponse{
    
    private HttpURLConnection connection;
    private List<Header> headers;
    
    /**
     * Creates a new instance of HTTPResponse
     */
    HTTPResponse(HttpURLConnection connection) {
        this.connection = connection;
    }
    
    /**
     * Gets status code of the response
     * @return a status code of the response
     * @throws java.io.IOException if an error occurs
     */
    public int getStatusCode() throws IOException{
        return connection.getResponseCode();
    }
    
    /**
     * Gets status message of an response
     * @return a status message of an response
     * @throws java.io.IOException if an error occurs
     */
    public String getStatusMessage() throws IOException {
        return connection.getResponseMessage();
    }
    
    /**
     * Gets Content Type of a response
     * @return a Content Type of a response
     */
    public String getContentType() {
        return connection.getContentType();
    }
/*    
    public byte[] getContent() {
        
    }
*/    
    /**
     * Gets a content of a response as a String
     * @return a content of a response
     * @throws java.io.IOException if an error occurs
     * @throws java.io.FileNotFoundException if an invalid file was provided
     */
    public String getContentAsString() throws FileNotFoundException, IOException {
        
        BufferedReader br;
        
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        
        String line;
        String content = "";

        while ((line = br.readLine()) != null) { 
            content = content + line + "\n";
        }
        br.close();
        
        return content;
    }

    /**
     * Gets List of Headers
     * @return a List of Headers
     */
    public List<Header> getHeaders() {
        prepareHeadersList();
        return headers;
    }
    
    /**
     * Gets Header with specified name
     * @param name a name of a Header
     * @return a Header with specified name
     */
    public Header getHeader(String name) {
        return new Header(name, connection.getHeaderField(name));
    }
    
    /**
     * Gets URL if an response
     * @return an URL
     */
    public URL getURL() {
        return connection.getURL();
    }   
    
    HttpURLConnection getConnection(){
        return connection;
    }
    
            
    private void prepareHeadersList() {
        headers = new ArrayList<Header>();
        for (int i = 0 ; ; i++) {
            String key = connection.getHeaderFieldKey(i);
            String value = connection.getHeaderField(i);
            
            if ((key == null) && (value == null))
                break;
            if (key == null)
                continue;
            
            headers.add(new Header(key, value));
        }
    }


    
    
}